package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.R;
import net.zjitc.dto.DishDto;
import net.zjitc.entity.Dish;
import net.zjitc.entity.DishFlavor;
import net.zjitc.service.DishFlavorService;
import net.zjitc.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 请求 URL: http://localhost:8080/dish
     * 请求方法: POST
     * 保存
     *
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info("保存：" + dishDto);
        dishService.saveWithFlavor(dishDto);
        return R.success("保存成功");
    }

    /**
     * 请求 URL: http://localhost:8080/dish/page?page=1&pageSize=10&name=abc
     * 请求方法: GET
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        log.info("分页请求：" + page + "/" + pageSize + "/" + name);
        //在dishService中新建一个方法，这个方法对DishDto进行分页查询，即在前端页面上能够显示菜品分类名字
        Page<DishDto> dishDtoPage = dishService.pageWithCategoryName(page, pageSize, name);
        return R.success(dishDtoPage);
    }

    /**
     * 提供数据，填充等待修改的表单
     * 请求 URL: http://localhost:8080/dish/1397849739276890114
     * 请求方法: GET
     */
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id) {
        Dish dish = dishService.getById(id);
        //当前id为菜品id 要去查询口味信息
        //根据菜品id去查询口味，private Long dishId;
        // dishFlavor的id是什么？它是唯一标识符，表示这个口味和菜品的关系
        //目标为获得dishDto，当前有dish
        // 当前添加口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(list);
        return R.success(dishDto);

    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        log.info("更新:" + dishDto);
        dishService.updateWithFlavor(dishDto);
        return R.success("更新成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Dish::getStatus, 1);

        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> dishList = dishService.list(queryWrapper);

        List<DishDto> dishDtoList = dishList.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, item.getId());
            List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(list);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtoList);
    }
}
